package com.starbucks.opencase.tool;

import com.starbucks.opencase.config.configOC;
import com.starbucks.opencase.entity.entProperties;
import com.starbucks.opencase.request.reqUpdate;
import com.starbucks.opencase.request.reqUpdateSignature;
import com.starbucks.opencase.response.resUserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class toolUserAccount {
    @Autowired
    private JdbcTemplate con;
    @Autowired
    private configOC configOC;

    /**
     * 是否存在该手机号
     * @param phone
     * @return
     */
    public boolean exist(String uid,String phone){
        return con.queryForObject("select count(*) from user_login where (phone=? or uid=?) and `lock`=0", Integer.class,phone,uid)>0;
    }

    /**
     * 登录验证
     * @param uid
     * @param phone
     * @param password
     * @return
     */
    public boolean testPassword(String uid,String phone,String password){
        return con.queryForObject("select count(*) from user_login where (uid=? or phone=?) and password=?", Integer.class,uid,phone,password)>0;
    }

    /**
     * 查询口令
     * @param uid
     * @param phone
     * @return
     */
    public String queryCommand(String uid,String phone){
        return con.queryForObject("select command from user_login where uid=? or phone=?", String.class,uid,phone);
    }

    /**
     * 查询玩家的信息
     * @param uid
     * @param phone
     * @return
     */
    public resUserLogin queryInfo(String uid, String phone){
        List<resUserLogin> re=con.query("""
                select
                a.uid,
                a.phone,
                b.avatar,
                b.nickname,
                b.gender,
                b.signature,
                b.address,
                b.identity,
                b.wx,
                c.energy,
                '登录成功' as tip,
                1 as status,
                '' as command
                from
                user_login as a 
                join user_info as b on a.uid=b.uid 
                join user_wallet as c on a.uid=c.uid
                where
                (a.uid=? or a.phone=?)
                """,new BeanPropertyRowMapper<>(resUserLogin.class),uid,phone);
        if(re.isEmpty())return null;
        return re.get(0);
    }

    /**
     * 查询所有账户数量
     * @return
     */
    public int queryAccountNum(){
        return con.queryForObject("select count(*) from user_login", Integer.class);
    }

    /**
     * 创建新用户
     * @param uid
     * @param phone
     */
    public void createNewAccount(String uid, String phone, entProperties properties){
        con.update("insert into user_login values(?,?,'123456','',0)",uid,phone);
        con.update("insert into user_info values (?,?,'','',-1,'',0,'','')",uid,properties.getNuNickname().replace("\\{uid\\}",uid));
        con.update("insert into user_wallet values(?,?)",uid, properties.getNuEnergy());
        con.update("insert into user_pet_bag values(?,?,0)",uid,properties.getNuPid());
    }

    /**
     * 新用户更新数据
     * @param info
     * @return
     */
    public boolean update(reqUpdate info){
        return con.update("update user_info set nickname=?,address=?,avatar=?,gender=?,signature=? where uid=?",
                info.getNickname(),info.getAddress(),info.getAvatar(),info.getGender(),info.getSignature(),info.getUid())>0;
    }

    /**
     * 更新用户昵称
     * @param uid
     * @param phone
     * @param nickname
     * @return
     */
    public boolean updateNickname(String uid,String phone,String nickname){
        return con.update("update user_info as a join user_login as b on a.uid=b.uid set a.nickname=? where a.uid=? or b.phone=?",nickname,uid,phone)>0;
    }

    /**
     * 更新用户性别
     * @param uid
     * @param phone
     * @param gender
     * @return
     */
    public boolean updateGender(String uid,String phone,int gender){
        return con.update("update user_info as a join user_login as b on a.uid=b.uid set a.gender=? where a.uid=? or b.phone=?",gender,uid,phone)>0;
    }

    /**
     * 更新头像
     * @param uid
     * @param phone
     * @param avatar
     * @return
     */
    public boolean updateAvatar(String uid,String phone,String avatar){
        return con.update("update user_info as a join user_login as b on a.uid=b.uid set a.avatar=? where a.uid=? or b.phone=?",avatar,uid,phone)>0;
    }

    /**
     * 更新地址
     * @param uid
     * @param phone
     * @param address
     * @return
     */
    public boolean updateAddress(String uid,String phone,String address){
        return con.update("update user_info as a join user_login as b on a.uid=b.uid set a.address=? where a.uid=? or b.phone=?",address,uid,phone)>0;
    }

    /**
     * 更新个性签名
     * @param uid
     * @param phone
     * @param signature
     * @return
     */
    public boolean updateSignature(String uid,String phone,String signature){
        return con.update("update user_info as a join user_login as b on a.uid=b.uid set a.signature=? where a.uid=? or b.phone=?",signature,uid,phone)>0;
    }
    /**
     * 更新个性签名
     * @param info
     * @return
     */
    public boolean updateSignature(reqUpdateSignature info){
        return con.update("update user_info set signature=? where uid=?",info.getSignature(),info.getUid())>0;
    }

    /**
     * 查询uid
     * @param phone
     * @return
     */
    public String queryUId(String phone){
        return con.queryForObject("select uid from user_login where phone=? and `lock`=0", String.class,phone);
    }
    public String queryNickname(String uid){
        return con.queryForObject("select nickname from user_info where uid=?", String.class,uid);
    }
    public int queryGender(String uid){
        return con.queryForObject("select gender from user_info where uid=?", Integer.class,uid);
    }
}
